﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
namespace DAL
{
    public class VisitRecordDAL
    {
        public static int Save(VisitRecordInfo info)
        {
            string sql = string.Format("insert into [visit_record]([SID],[UserID],[Province],[City],[Area],[Lat],[Lng],[Address],[CreateID],[CreateDate],[FCount],[Flag]) values({0},{1},'{2}','{3}','{4}',{5},{6},'{7}',{8},'{9}',{10},{11});select @@identity;", info.SID, info.UserID, info.Province, info.City, info.Area, info.Lat, info.Lng, info.Address, info.CreateID, info.CreateDate, info.FCount,info.Flag);
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql));
        }

        public static List<dynamic> FindData(int userid, string begindate, string enddate)
        {
            string filter = " CreateID="+userid;
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter += " and ([prize_record].CreateDate between '" + DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00") + "' and '" + DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59") + "' or convert(char(10),CreateDate,120)=convert(char(10),getdate(),120))";
            }
            DataTable dt = CommonDAL.GetDataTable(string.Format("select sum(fcount) as OpenCount,convert(char(10),CreateDate,120) as CreateDate from [visit_record] where {0} group by convert(char(10),CreateDate,120)", filter));
            List<dynamic> list = new List<dynamic>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Single_Data(row));
            }
            return list;
        }
        public static List<dynamic> FindData2(int userid, string begindate, string enddate)
        {
            string filter = " Flag=2 and CreateID=" + userid;
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter += " and ([prize_record].CreateDate between '" + DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00") + "' and '" + DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59") + "' or convert(char(10),CreateDate,120)=convert(char(10),getdate(),120))";
            }
            DataTable dt = CommonDAL.GetDataTable(string.Format("select sum(fcount) as ShareCount,convert(char(10),CreateDate,120) as CreateDate from [visit_record] where {0} group by convert(char(10),CreateDate,120)", filter));
            List<dynamic> list = new List<dynamic>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Single_Data2(row));
            }
            return list;
        }
        public static bool IsVisit(int activeid, int userid)
        {
            int count = DataConvert.ToInt(CommonDAL.ExecuteScalar(string.Format("select count(1) from [visit_record] where SID={0} and UserID={1}", activeid, userid)));
            return count > 0 ? true : false;
        }
        private static dynamic Single_Data(DataRow dr)
        {
            dynamic info = new ExpandoObject();
            if (dr != null)
            {
                info.OpenCount = DataConvert.ToInt(dr["OpenCount"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static dynamic Single_Data2(DataRow dr)
        {
            dynamic info = new ExpandoObject();
            if (dr != null)
            {
                info.ShareCount = DataConvert.ToFloat(dr["ShareCount"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static VisitRecordInfo Simple_Data(DataRow dr)
        {
            VisitRecordInfo info = new VisitRecordInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.SID = DataConvert.ToInt(dr["SID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.Province = DataConvert.ToString(dr["Province"]);
                info.City = DataConvert.ToString(dr["City"]);
                info.Area = DataConvert.ToString(dr["Area"]);
                info.Lat = DataConvert.ToFloat(dr["Lat"]);
                info.Lng = DataConvert.ToFloat(dr["Lng"]);
                info.Address = DataConvert.ToString(dr["Address"]);
                info.Flag = DataConvert.ToInt(dr["Flag"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        
    }
}
